
package com.linkmed.imgserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Map;
import java.util.TreeMap;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.handler.stream.StreamIoHandler;

public class ImgProtocolHandler extends StreamIoHandler {

	protected void processStreamIo(IoSession session, InputStream in, OutputStream out) {
		// You *MUST* execute stream I/O logic in a separate thread.
		new Imager(in, out).start();
	}

	private static class Imager extends Thread {

		private final InputStream in;

		private final OutputStream out;

		public Imager(InputStream in, OutputStream out) {
			setDaemon(true);
			this.in = in;
			this.out = out;
		}

		public void run() {
			String url;
			Map headers = new TreeMap();
			BufferedReader in = new BufferedReader(new InputStreamReader(this.in));
			File file = new File("/home/wutao/图片/11365600.jpg");
			try {
				byte[] b = new byte[1024];
				InputStream imagein = new FileInputStream(file);
				while (true) {
					if (imagein.read(b) == -1) {
						break;
					}
					this.out.write(b, 0, b.length);
				}
				System.out.println("test");

			} catch (Exception ex) {
				ex.printStackTrace();
			} finally {
				try {
					this.out.flush();
					this.out.close();
					in.close();
				} catch (IOException e) {
				}
			}
		}
	}
}